$(function () {
    var app = new Vue({
        el:'#container',
        data:goods_info
    });
    //选中改变属性
    app.xuan_zhong = function ($this,val) {

        var par = $this.parents('li');
        par.parents('ul').find('input').val(val);//添加input的值
        //改变背景
        par.find('a').removeClass('this');
        $this.addClass('this');

        //开始 赋值到vue
        //attribute
        app.attribute[par.parent().index()] = val;

        //把数组变成字符串在把“,”替换成“-”
        //比对 库存-价格
        var arr_toS = app.attribute.toString().replace(/\,/,'-');
        if('undefined' != typeof app.goods_info[arr_toS]){
            //修改 库存-价格
            app.ku_cun = app.goods_info[arr_toS].split('-')[0];
            app.jia_ge = app.goods_info[arr_toS].split('-')[1];

            //判断购买件数 是否大于库存
            if(app.ku_cun < app.count){
                app.count = 1;
            }
        }
        //console.log(typeof app.goods_info[arr_toS]);

    };

//数量加减   false:件 true:加
    app.ja = function (state) {
        var pare = app.count;
        if(state){
            app.count = pare+=1;
            app.count = Math.min(app.ku_cun,app.count)
        }
        else{
            app.count = pare-=1;
            app.count = Math.max(1,app.count)
        }
    };

    //输入购买件数事件
    app.input_chan = function () {
        if(app.count < 1) app.count = 1;//小于1 等于1
        if(app.count > app.ku_cun) app.count =app.ku_cun;//大于库存 等于库存
    };

    //点击加入购物车或
    app.btn = function (blo) {
        //判断登入没
        if(!public_top.state){
            location.href=DURL+'/login.html';
            return false;
        }
        app.state = blo;//点击类型赋值
        $('[name="type"]').val(app.state);
        //判断属性是否全选
        $('#attribute-value >ul input').each(function (i,n) {
            if ( $(this).val() == '' ) {
                layer.msg('请选择属性',{icon:2});
                return false;
            }else{
                //判断 遍历到最后一个
                if((i+1) == $('#attribute-value >ul input').length ){
                    post();
                }
            }

        });


        //判断加入购物车 和立即购买
        function post() {
            if (!app.state) {
                layer.msg('已加入购物车！', {icon: 1});
                public_top.count = public_top.count+1;
            }
            else {
                layer.load(1, {shade: [0.2, '#333']});
            }
            $.post(DORDER+'/post_Shopping',$('#form').serialize(),function (da) {
                if(app.state){
                    $('#goodsid').val(da);//添加id
                    $('#bottomOrder').submit();//执行下单
                }

            });

        }
    };

});




